import { createRouter, createWebHistory } from 'vue-router'
// import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    // {
    //   path: '/',
    //   name: 'home',
    //   component: HomeView,
    // },
    {
      path: '/',
      redirect: '/home/index',
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/login/index.vue'),
    },
    {
      path: '/home',
      name: 'home',
      component: () => import('@/views/home/index.vue'),
      children: [
        {
          path: 'index',
          name: 'index',
          component: () => import('@/views/home/children/index.vue'),
        },
        {
          path: 'clas',
          name: 'clas',
          component: () => import('@/views/home/children/clas.vue'),
        },
        {
          path: 'shop',
          name: 'shop',
          component: () => import('@/views/home/children/shop.vue'),
        },
        {
          path: 'my',
          name: 'my',
          component: () => import('@/views/home/children/my.vue'),
        },
      ]
    },
  ],
})

// GOOD
router.beforeEach((to, from, next) => {
  let isAuthenticated = localStorage.getItem('token')
  if ((to.name == 'shop' || to.name == 'my') && !isAuthenticated) next({ name: 'login' })
  else next()
})

export default router
